package com.itheima.controller.store;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Company;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/*
反射获取当前访问路径
 */
@WebServlet("/store/catalog/*")
public class CatalogServlet extends BaseServlet {
    //查询所有(分页)
    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前页码和每页显示数
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        //调用业务层查找所有方法
        List<Catalog> list=catalogService.findAll(pageNum,pageSize);
        PageInfo<Catalog> pageInfo=new PageInfo<Catalog>(list);
        req.setAttribute("page",pageInfo);
//        将pageinfo重定向到list.jsp页面
        req.getRequestDispatcher("/pages/store/catalog/list.jsp").forward(req,resp);
    }
    //获取不带分页信息的list
    private void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Catalog> list=catalogService.list();
        ObjectMapper obj = new ObjectMapper();
        String json = obj.writeValueAsString(list);
        resp.getWriter().write(json);
    }
    //新增
    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前新增对象并封装到catalog中
        Map<String, String[]> map = req.getParameterMap();
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class, "yyyy-MM-dd");
        //调用业务层新增方法
        catalogService.save(catalog);
        //新增完成后再次调用servlet查找所有方法
        findAll(req,resp);
    }
    //删除
    private void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取要删除的ids(是个字符串,可以转成数组)
        String id = req.getParameter("ids");
        System.out.println(id);
        //调用业务层删除方法
        boolean flag=catalogService.deleteByIds(id);
        if(flag){
            //删除成功则调用servlet查找所有方法
            findAll(req,resp);
        }else{
            //失败则跳转到错误页面
            req.getRequestDispatcher("/pages/error.jsp").forward(req,resp);
        }
    }
    //编辑第一次点击实际是根据id获取对象,并把id传给编辑框的对象
    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取当前id
        String id = req.getParameter("id");
        Catalog catalog=catalogService.findById(id);
//        System.out.println(catalog);
        req.setAttribute("catalog",catalog);
        req.getRequestDispatcher("/pages/store/catalog/update.jsp").forward(req,resp);
    }

   //编辑
    private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取修改页面表单提交的数据，并封装到Catalog对象
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class);
        catalogService.update(catalog);
        findAll(req,resp);
    }
}
